package zhongbo.java.air.dictionary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import zhongbo.java.air.dictionary.mapper.AirAlertZoneMapper;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.IAirAlertZoneService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AirAlertZoneServiceImpl extends ServiceImpl<AirAlertZoneMapper, AirAlertZone> implements IAirAlertZoneService {

    @Override
    public Page<AirAlertZone> pageWithOrder(Page<AirAlertZone> page, String orderField, boolean isAsc) {
        QueryWrapper<AirAlertZone> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public IPage<AirAlertZone> pageWithOrder(IPage<AirAlertZone> page, String orderField, boolean isAsc) {
        QueryWrapper<AirAlertZone> queryWrapper = new QueryWrapper<>();
        
        if (orderField != null && !orderField.isEmpty()) {
            queryWrapper.orderBy(true, isAsc, orderField);
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        
        return page(page, queryWrapper);
    }

    @Override
    public AirAlertZone getByZoneCode(String zoneCode) {
        LambdaQueryWrapper<AirAlertZone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirAlertZone::getZoneCode, zoneCode);
        return getOne(queryWrapper);
    }

    @Override
    public List<AirAlertZone> listByZoneType(String zoneType) {
        LambdaQueryWrapper<AirAlertZone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirAlertZone::getZoneType, zoneType);
        return list(queryWrapper);
    }

    @Override
    public List<AirAlertZone> listEffectiveAt(LocalDateTime dateTime) {
        LambdaQueryWrapper<AirAlertZone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(AirAlertZone::getEffectiveStart, dateTime)
                .and(q -> q.isNull(AirAlertZone::getEffectiveEnd)
                        .or()
                        .ge(AirAlertZone::getEffectiveEnd, dateTime));
        return list(queryWrapper);
    }
    
    @Override
    public boolean save(AirAlertZone entity) {
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        return super.save(entity);
    }
    
    @Override
    public boolean updateById(AirAlertZone entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return super.updateById(entity);
    }
}
